package com.g4.shoppingback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.g4.shoppingback.common.PageVo;
import com.g4.shoppingback.exception.ServiceException;
import com.g4.shoppingback.mapper.PermissionMapper;
import com.g4.shoppingback.mapper.RolePermissionMapper;
import com.g4.shoppingback.mapper.UserRoleMapper;
import com.g4.shoppingback.pojo.DdUsersInfo;
import com.g4.shoppingback.pojo.Role;
import com.g4.shoppingback.mapper.RoleMapper;
import com.g4.shoppingback.pojo.RolePermission;
import com.g4.shoppingback.pojo.UserRole;
import com.g4.shoppingback.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-02-19
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 根据分页信息或角色名查询角色
     * @param pageVo 封装了查询关键字和页数
     * @return 返回分页信息
     */
    @Override
    public PageVo selectRoleByPage(PageVo pageVo) {
        Integer pageSize = 5;   //设置每页固定显示5条数据
        Integer pageStart = (pageVo.getPageNum()-1)*pageSize;   //设置从第几条开始
        Long total = roleMapper.selectTotal(pageVo.getQuery());  //查询总条数
        List<Role> roleList = roleMapper.selectRoleByPage(pageVo.getQuery(),pageStart,pageSize);
        pageVo.setPageSize(pageSize).setTotal(total).setRows(roleList); //封装分页信息
        return pageVo;
    }

    /**
     * 根据id查询角色
     * @param id 角色id
     * @return 返回单个用户信息
     */
    @Override
    public Role selectRoleById(Integer id) {
        Role role = roleMapper.selectRoleById(id);
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(queryWrapper);
        ArrayList<Integer> list = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissions) {
            list.add(rolePermission.getPermissionId());
        }
        role.setPermissionIds(list);
        System.out.println(role);
        return role;
    }

    /**
     * 根据姓名查询角色
     * @param username
     * @return 返回角色信息
     */
    @Override
    public String selectRoleByName(String username) {
        return roleMapper.selectRoleByName(username);
    }

    /**
     * 查询所有角色
     * @return 多个角色
     */
    @Override
    public List<Role> selectRoles() {
        return roleMapper.selectRoles();
    }

    /**
     * 新增角色
     * @param role 传入角色对象
     * @return 返回影响行数
     */
    @Override
    public int addRole(Role role) {
        int row = roleMapper.insert(role);
        if(row==1){
            addRolePermission(role);
            return 1;
        }return 0;

    }

    /**
     * 根据角色id删除角色
     * @param id 角色id
     * @return 返回影响行数
     */
    @Override
    public int deleteRoleById(Integer id) {
        QueryWrapper<UserRole> qw = new QueryWrapper();
        qw.eq("role_Id",id);
        UserRole userRole = userRoleMapper.selectOne(qw);
        if(userRole!=null) return 2;
        int row = roleMapper.deleteRoleById(id); //删除角色信息
        if(row==1){
            QueryWrapper<RolePermission> queryWrapper = new QueryWrapper();
            queryWrapper.eq("role_Id",id);
            //根据角色名找到所有角色
            List<RolePermission> rolePermissions = rolePermissionMapper.selectList(queryWrapper);
            //删除相关角色权限关系
            deleteRolePermission(rolePermissions);
            return 1;
        }return 0;
    }

    /**
     * 修改角色
     * @param role 传入角色对象
     * @return 返回影响行数
     */
    @Override
    public int updateRole(Role role) {
        int row = roleMapper.updateById(role); //修改角色信息
        if(row==1){
            QueryWrapper<RolePermission> queryWrapper = new QueryWrapper();
            queryWrapper.eq("role_Id",role.getId());
            //根据角色名找到所有角色
            List<RolePermission> rolePermissions = rolePermissionMapper.selectList(queryWrapper);
            //删除所有角色权限关系
            for (RolePermission rolePermission : rolePermissions) {
                int rows = rolePermissionMapper.deleteById(rolePermission.getId());
                if(rows==0) throw new ServiceException("角色权限关系删除失败");
            }
            //重新添加角色权限关系
            addRolePermission(role);
            return 1;
        }return 0;
    }

    //新增角色权限关系
    private void addRolePermission(Role role) {
        for (Integer permissionId : role.getPermissionIds()) {
            RolePermission rolePermission = new RolePermission().setRoleId(role.getId()).setPermissionId(permissionId);
            int rows = rolePermissionMapper.insert(rolePermission);
            if(rows==0) throw new ServiceException("角色权限关系添加失败");
        }
    }

    //删除相关角色权限关系
    private void deleteRolePermission(List<RolePermission> rolePermissions) {
        for (RolePermission rolePermission : rolePermissions) {
            int rows = rolePermissionMapper.deleteById(rolePermission.getId());
            if(rows==0) throw new ServiceException("角色权限关系删除失败");
        }
    }

}
